package com.ruoyi.web.group.training.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.web.group.training.entyiy.TrainingSession;
import com.ruoyi.web.group.training.service.TrainingSessionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 训练控制器
 */
@RestController
@RequestMapping("/api/training-sessions")
public class TrainingSessionController {

    @Autowired
    private TrainingSessionService trainingSessionService;

    // 创建训练课程
    @PostMapping
    public AjaxResult create(@RequestBody TrainingSession trainingSession) {
        boolean saved = trainingSessionService.save(trainingSession);
        return saved ? AjaxResult.success(trainingSession) : AjaxResult.error("创建失败");
    }

    // 更新训练课程
    @PostMapping("/{id}")
    public AjaxResult update(@PathVariable Integer id, @RequestBody TrainingSession trainingSession) {
        trainingSession.setSessionId(id);
        boolean updated = trainingSessionService.updateById(trainingSession);
        return updated ? AjaxResult.success(trainingSession) : AjaxResult.error("更新失败");
    }

    // 删除训练课程
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable Integer id) {
        boolean removed = trainingSessionService.removeById(id);
        return removed ? AjaxResult.success(null) : AjaxResult.error("删除失败");
    }

    // 获取单个训练课程详情
    @GetMapping("/{id}")
    public AjaxResult getById(@PathVariable Integer id) {
        TrainingSession session = trainingSessionService.getById(id);
        return session != null ? AjaxResult.success(session) : AjaxResult.error("未找到该记录");
    }

    // 分页查询训练课程
    @GetMapping
    public AjaxResult list(@RequestParam(defaultValue = "1") Integer pageNum,
                      @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<TrainingSession> page = trainingSessionService.page(new Page<>(pageNum, pageSize));
        return AjaxResult.success(page);
    }

    // 根据球员ID查询训练课程
    @GetMapping("/player/{playerId}")
    public AjaxResult listByPlayerId(@PathVariable Integer playerId) {
        List<TrainingSession> sessions = trainingSessionService.listByPlayerId(playerId);
        return AjaxResult.success(sessions);
    }
}